<?php
// +----------------------------------------------------------------------
// | Leaps Framework [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011-2014 Leaps Team (http://www.tintsoft.com)
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author XuTongle <xutongle@gmail.com>
// +----------------------------------------------------------------------

use Leaps\HttpClient;

class Google {

	/**
	 * http client 客户端类
	 * @var Leaps\HttpClient
	 */
	private $http;

	/**
	 * 构造方法
	 */
	public function __construct()
	{
		$this->http = new HttpClient ();
	}

	/**
	 * 获取收录数量
	 */
	public function getSite($domain)
	{
		$url = "https://www.google.com/search?hl=zh-CN&source=hp&q=site%3A{$domain}&gbv=2&oq=site%3A{$domain}";
		return $this->handle ( $url );
	}

	/**
	 * 获取反联数量
	 *
	 * @param string $domain
	 */
	public function getLink($domain)
	{
		$url = "https://www.google.com.hk/search?q=link%3A{$domain}&hl=zh-CN&gbv=2&oq=&gs_l=";
		return $this->handle ( $url );
	}

	/**
	 * 处理请求
	 */
	private function handle($url)
	{
		//模拟IE8 自动屏蔽Google特效和ajax
		$this->http->setUserAgent('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E) ');
		$response = $this->http->get ( $url );
		if ($response->getHttpCode () == '200') {
			$mode = '/找到相关结果约(.*)个/U';
			preg_match ( $mode, $response->getBody (), $result );
			return $result [1];
		} else {
			return 0;
		}
	}
}